package com.kobeliu.controller;

import com.kobeliu.config.MinIOConfig;
import com.kobeliu.config.MinIOUtils;
import com.kobeliu.config.OSSConfig;
import com.kobeliu.config.OSSUtils;
import com.kobeliu.pojo.bo.Base64FileBO;
import com.kobeliu.result.GraceJSONResult;
import com.kobeliu.result.ResponseStatusEnum;
import com.kobeliu.utils.Base64ToFile;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

@RestController
@RequestMapping("/file")
@Slf4j
public class FileController {

    @Autowired
    private OSSConfig ossConfig;

    @Autowired
    private MinIOConfig minIOConfig;

    /**
     * springboot 上传本地资源
     * @param file
     * @param userId
     * @param request
     * @return
     * @throws IOException
     */
    @PostMapping("upload1")
    public GraceJSONResult uploadFace1(@RequestParam("file")MultipartFile file,
                                      @RequestParam("userId") String userId,
                                      HttpServletRequest request) throws IOException {
        //获得文件原始名称
        String fileName = file.getOriginalFilename();

        //获取后缀名
        String suffixName = fileName.substring(fileName.lastIndexOf("."));

        //创建一个新的名称
        String newFileName = userId+suffixName;

        //设置文件存储路径
        String rootPath = "/temp"+ File.separator;

        String filePath = rootPath+File.separator+"face"+File.separator+newFileName;

        File newFile = new File(filePath);
        if(!newFile.getParentFile().exists()){
            //如果目标文件所在目录不存在，则创建父目录
            newFile.getParentFile().mkdir();
        }

        //将内存中的文件数据写入硬盘
        file.transferTo(newFile);

        return  GraceJSONResult.ok();
    }

    /**
     * minio存储
     * @param file
     * @param userId
     * @return
     * @throws Exception
     */
    @PostMapping("upload2")
    public GraceJSONResult uploadFace2(@RequestParam("file")MultipartFile file,
                                      @RequestParam("userId") String userId) throws Exception {

        if(StringUtils.isBlank(userId)){
            return GraceJSONResult.errorCustom(ResponseStatusEnum.FILE_UPLOAD_FAILD);
        }

        //文件初始名称
        String filename = file.getOriginalFilename();
        if(StringUtils.isBlank(filename)){
            return GraceJSONResult.errorCustom(ResponseStatusEnum.FILE_UPLOAD_NULL_ERROR);
        }

        filename = userId+File.separator+filename;

        //流上传
        MinIOUtils.uploadFile(minIOConfig.getBucketName(),filename,file.getInputStream());

        String imageUrl = minIOConfig.getFileHost()+"/"+minIOConfig.getBucketName()+"/"+filename;

        return GraceJSONResult.ok(imageUrl);

    }

    /**
     * OSS云存储
     * @param file
     * @param userId
     * @return
     * @throws Exception
     */
    @PostMapping("upload")
    public GraceJSONResult uploadFace(@RequestParam("file")MultipartFile file,
                                      @RequestParam("userId") String userId) throws Exception {

        if(StringUtils.isBlank(userId)){
            return GraceJSONResult.errorCustom(ResponseStatusEnum.FILE_UPLOAD_FAILD);
        }

        //文件初始名称
        String filename = file.getOriginalFilename();
        if(StringUtils.isBlank(filename)){
            return GraceJSONResult.errorCustom(ResponseStatusEnum.FILE_UPLOAD_NULL_ERROR);
        }

        filename = userId+File.separator+filename;

        //流上传
        OSSUtils.uploadFile(file,filename);

        String imageUrl = ossConfig.getFileHost()+"/"+filename;

        return GraceJSONResult.ok(imageUrl);

    }

    /**
     * 上传公司logo
     * @param file
     * @return
     * @throws Exception
     */
    @PostMapping("uploadLogo")
    public GraceJSONResult uploadLogo(@RequestParam("file")MultipartFile file) throws Exception {

        //文件初始名称
        String filename = file.getOriginalFilename();
        if(StringUtils.isBlank(filename)){
            return GraceJSONResult.errorCustom(ResponseStatusEnum.FILE_UPLOAD_NULL_ERROR);
        }

        filename = "/company/"+filename;

        //流上传
        MinIOUtils.uploadFile(minIOConfig.getBucketName(),filename,file.getInputStream());

        String imageUrl = minIOConfig.getFileHost()+"/"+minIOConfig.getBucketName()+"/"+filename;

        return GraceJSONResult.ok(imageUrl);

    }

    /**
     * 上传营业执照
     * @param file
     * @return
     * @throws Exception
     */
    @PostMapping("uploadBizLicense")
    public GraceJSONResult uploadBizLicense(@RequestParam("file")MultipartFile file) throws Exception {

        //文件初始名称
        String filename = file.getOriginalFilename();
        if(StringUtils.isBlank(filename)){
            return GraceJSONResult.errorCustom(ResponseStatusEnum.FILE_UPLOAD_NULL_ERROR);
        }

        filename = "/company/bizLicence/"+filename;

        //流上传
        MinIOUtils.uploadFile(minIOConfig.getBucketName(),filename,file.getInputStream());

        String imageUrl = minIOConfig.getFileHost()+"/"+minIOConfig.getBucketName()+"/"+filename;

        return GraceJSONResult.ok(imageUrl);

    }

    /**
     * 上传授权书
     * @param file
     * @return
     * @throws Exception
     */
    @PostMapping("uploadAuthLetter")
    public GraceJSONResult uploadAuthLetter(@RequestParam("file")MultipartFile file) throws Exception {

        //文件初始名称
        String filename = file.getOriginalFilename();
        if(StringUtils.isBlank(filename)){
            return GraceJSONResult.errorCustom(ResponseStatusEnum.FILE_UPLOAD_NULL_ERROR);
        }

        filename = "/company/authLetter/"+filename;

        //流上传
        MinIOUtils.uploadFile(minIOConfig.getBucketName(),filename,file.getInputStream());

        String imageUrl = minIOConfig.getFileHost()+"/"+minIOConfig.getBucketName()+"/"+filename;

        return GraceJSONResult.ok(imageUrl);

    }

    @PostMapping("uploadAdminFace")
    public GraceJSONResult uploadAdminFace(@RequestBody Base64FileBO base64FileBO) throws Exception {

        //文件初始名称
        String file = base64FileBO.getBase64File();
        String suffix = ".png";
        String uuid = UUID.randomUUID().toString();
        String objectName = uuid + suffix;

        String rootPath = "d:\\temp"+ File.separator;
        String filePath = rootPath+File.separator+"adminFace"+File.separator+objectName;

        //先放到本地
        Base64ToFile.Base64ToFile(file,filePath);

        //上传minio
        MinIOUtils.uploadFile(minIOConfig.getBucketName(),objectName,filePath);


        String imageUrl = minIOConfig.getFileHost()
                +"/"
                +minIOConfig.getBucketName()
                +"/"
                +objectName;

        return GraceJSONResult.ok(imageUrl);

    }

    @PostMapping("uploadFace")
    public GraceJSONResult uploadFace(@RequestParam("file")MultipartFile file) throws Exception {

        //文件初始名称
        String filename = file.getOriginalFilename();
        if(StringUtils.isBlank(filename)){
            return GraceJSONResult.errorCustom(ResponseStatusEnum.FILE_UPLOAD_NULL_ERROR);
        }

        filename = "/userFace/"+filename;

        //流上传
        MinIOUtils.uploadFile(minIOConfig.getBucketName(),filename,file.getInputStream());

        String imageUrl = minIOConfig.getFileHost()+"/"+minIOConfig.getBucketName()+"/"+filename;

        return GraceJSONResult.ok(imageUrl);

    }

    @PostMapping("uploadPhoto")
    public GraceJSONResult uploadPhoto(@RequestParam("files")MultipartFile[] files,String companyId) throws Exception {

        List<String> fileList = new ArrayList<>();

        for(MultipartFile f:files){
            String filename = f.getOriginalFilename();
            if(StringUtils.isBlank(filename)){
                return GraceJSONResult.errorCustom(ResponseStatusEnum.FILE_UPLOAD_NULL_ERROR);
            }

            filename = "/company/photo/"+filename;
            //流上传
            MinIOUtils.uploadFile(minIOConfig.getBucketName(),filename,f.getInputStream());

            String imageUrl = minIOConfig.getFileHost()+"/"+minIOConfig.getBucketName()+"/"+filename;
            fileList.add(imageUrl);
        }
        return GraceJSONResult.ok(fileList);
    }

    @PostMapping("uploadArticleCover")
    public GraceJSONResult uploadArticleCover(@RequestParam("file")MultipartFile file) throws Exception {


        String filename = file.getOriginalFilename();
        if(StringUtils.isBlank(filename)){
            return GraceJSONResult.errorCustom(ResponseStatusEnum.FILE_UPLOAD_NULL_ERROR);
        }

        filename = "/article/covers/"+filename;
        //流上传
        MinIOUtils.uploadFile(minIOConfig.getBucketName(),filename,file.getInputStream());

        String imageUrl = minIOConfig.getFileHost()+"/"+minIOConfig.getBucketName()+"/"+filename;

        return GraceJSONResult.ok(imageUrl);

    }

    @PostMapping("uploadChatPhoto")
    public GraceJSONResult uploadChatPhoto(@RequestParam("file")MultipartFile file,String userId) throws Exception {

        //文件初始名称
        String filename = file.getOriginalFilename();
        if(StringUtils.isBlank(filename)){
            return GraceJSONResult.errorCustom(ResponseStatusEnum.FILE_UPLOAD_NULL_ERROR);
        }

        filename = "/chat/"+userId+"/photo/"+filename;

        //流上传
        MinIOUtils.uploadFile(minIOConfig.getBucketName(),filename,file.getInputStream());

        String imageUrl = minIOConfig.getFileHost()+"/"+minIOConfig.getBucketName()+"/"+filename;

        return GraceJSONResult.ok(imageUrl);

    }

    @PostMapping("uploadChatVideo")
    public GraceJSONResult uploadChatVideo(@RequestParam("file")MultipartFile file,String userId) throws Exception {

        //文件初始名称
        String filename = file.getOriginalFilename();
        if(StringUtils.isBlank(filename)){
            return GraceJSONResult.errorCustom(ResponseStatusEnum.FILE_UPLOAD_NULL_ERROR);
        }

        filename = "/chat/"+userId+"/video/"+filename;

        //流上传
        MinIOUtils.uploadFile(minIOConfig.getBucketName(),filename,file.getInputStream());

        String imageUrl = minIOConfig.getFileHost()+"/"+minIOConfig.getBucketName()+"/"+filename;



        return GraceJSONResult.ok(imageUrl);

    }

    @PostMapping("uploadChatVoice")
    public GraceJSONResult uploadChatVoice(@RequestParam("file")MultipartFile file,String userId) throws Exception {

        //文件初始名称
        String filename = file.getOriginalFilename();
        if(StringUtils.isBlank(filename)){
            return GraceJSONResult.errorCustom(ResponseStatusEnum.FILE_UPLOAD_NULL_ERROR);
        }

        filename = "/chat/"+userId+"/voice/"+filename;

        //流上传
        MinIOUtils.uploadFile(minIOConfig.getBucketName(),filename,file.getInputStream());

        String imageUrl = minIOConfig.getFileHost()+"/"+minIOConfig.getBucketName()+"/"+filename;

        return GraceJSONResult.ok(imageUrl);

    }



}
